import axios from 'axios'

const passportService = axios.create({
  baseURL: '/passport',
  timeout: 5000
})

// 请求拦截器
passportService.interceptors.request.use(
  (config) => {
    // 在发送请求前添加认证头
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
passportService.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    // 统一处理错误
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 处理未授权
          break
        case 404:
          // 处理未找到
          break
        default:
          // 处理其他错误
      }
    }
    return Promise.reject(error)
  }
)

export default passportService